﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using WindowsTheme.Helper;

namespace WindowsTheme.ThemeHelper
{
    public partial class Copyright : Form
    {
        public Copyright()
        {
            InitializeComponent();
            InitView();
        }

        private void InitView()
        {
            textBox2.Text = DateTime.Now.ToLongDateString().ToString();
      
            InitData();
        }

        private void InitData()
        {
            String XmlPath = StaticValue.ThemeFloder + "ThemeInfo.xml";
            Console.WriteLine(XmlPath);
            XmlDocument xmlDocument = new XmlDocument();
            if (File.Exists(XmlPath))
            {
                xmlDocument.Load(XmlPath);
            }
            else
            {
                xmlDocument.LoadXml(Resource1.UserInfoXml);
            }
            textBox1.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/username"));
            textBox3.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/version"));
            textBox4.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/statement"));
            textBox5.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/winthemename"));
            textBox6.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/themepublicitylink"));
            textBox7.Text = XmlHelper.SelectFirstValue(XmlHelper.SelectNode(xmlDocument.InnerXml, "userinfo/themepublicity"));


        }

        private void AutoFillText()
        {
            textBox4.Text = TextHelper.ReaderText(StaticValue.CopyrightPath1);
            textBox1.Text = TextHelper.ReaderText(StaticValue.CopyrightUserName1);

        }
        /*
         <?xml version="1.0" encoding="utf-8"?>  
<UserInfo>  
<Username>未知作者</Username>  
<Themename>未命名</Themename>  
  <version>1.0</version>
 <statement>默认协议</statement>  
  <version>1.0</version>
  <createtime>未知</createtime>  
    </UserInfo>  
             */

        private void button1_Click(object sender, EventArgs e)
        {
            if (VerificationTextBox())
            {//保存如下两个值到文件以便下次读取
                TextHelper.SaveText(StaticValue.CopyrightUserName1, textBox1.Text);
                TextHelper.SaveText(StaticValue.CopyrightPath1, textBox4.Text);
                XmlDocument Info = new XmlDocument();
                String XmlPath = StaticValue.ThemeFloder + "ThemeInfo.xml";
                if (File.Exists(XmlPath))
                {
                    Info.Load(XmlPath);
                }
                else {
                    Info.LoadXml(Resource1.UserInfoXml);
                }
               
                Info = XmlHelper.ModifyXml(Info, "userinfo/username", textBox1.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/createtime", textBox2.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/version", textBox3.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/statement", textBox4.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/winthemename", textBox5.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/themepublicitylink", textBox6.Text);
                Info = XmlHelper.ModifyXml(Info, "userinfo/themepublicity", textBox7.Text);
               
                Info.Save(XmlPath);


            }
            else
            {
                MessageBox.Show("请完整填写所有信息！");
            }



        }
        /// <summary>
        /// 判断textbox是否填完了
        /// </summary>
        /// <returns></returns>
        private bool VerificationTextBox()
        {
            if (textBox1.Text != "" &&
                   textBox2.Text != "" &&
                   textBox3.Text != "" &&
                   textBox4.Text != "" &&
                    textBox5.Text != "" &&
                     textBox7.Text != "" &&
                     textBox6.Text != "")
                return true;
            else
                return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            if (MessageHelper.MessageShow("删除缓存提示", "是否也同时删除用于自动填充的记录文件？"))
                InformationDelete();

        }

        private void InformationDelete()
        {
            try
            {
                File.Delete(StaticValue.CopyrightPath1);
                File.Delete(StaticValue.CopyrightUserName1);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new ImgTip(Resource1.alert).ShowDialog();
        }

        private void textBox6_Click(object sender, EventArgs e)
        {
            if (MessageHelper.MessageShow("清空文本", "是否清空文本？"))

                textBox6.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (File.Exists(StaticValue.CopyrightPath1))
            {


                if (MessageHelper.MessageShow("发现您之前保存的宣传信息", "加载上次的记录吗?"))//如果点击“确定”按钮

                {
                    AutoFillText();
                }


            }
        }
    }
}
